<?php 
namespace Product\Model;

 use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Predicate\In;
	
 class ImgProductTable
 {
     protected $tableGateway;

     public function __construct(TableGateway $tableGateway)
     {
         $this->tableGateway = $tableGateway;
     }

     public function getImgProduct($id)
     {
         /* $id  = (int) $id;
         $rowset = $this->tableGateway->select(array('ID_PR' => $id));
         
         $row = $rowset->current();
         if (!$row) {
             throw new \Exception("Không thể tìm thấy hàng $id");
         }
         
         return $row; */
     	$id  = (int) $id;
     	$select = $this->tableGateway->getSql()->select();
     	$select->where(array('ID_PR'=>$id));
     	$resultSet = $this->tableGateway->selectWith ( $select );
     	$resultSet->buffer ();
     	return $resultSet;
     }

     public function getAllImgProduct($arrid)
     {
     	$where = new In('ID_PR',$arrid);
     	$select = $this->tableGateway->select($where);
     	return $select;
     	
     }
     
     public function saveImgProduct($arrlink,$idprtype)
     {
     	$data =array();
     	foreach ($arrlink as $key => $val){
         $data[] = array(
			 'LINKIMG' => $val ,
			 'ID_PR' => $idprtype ,
         );
         
     	}
     	foreach ($data as $arrdata)
     	    $this->tableGateway->insert($arrdata);
        /*  $id = (int) $imgproduct->ID_IMGPR;
         if ($id == 0) {
             $this->tableGateway->insert($data);
            
         } else {
             if ($this->getProduct($id)) {
                 $this->tableGateway->update($data, array('ID_PR' => $id));
             } else {
                 throw new \Exception('Product id does not exist');
             }
         } */
     }
  
     public  function deleteimgpro($id){
     	$this->tableGateway->delete(array('ID_IMGPR' => (int) $id));
     }
     
     public function deleteImgProduct($id)
     {
     	$this->tableGateway->delete(array('ID_PR' => (int) $id));
     }
     
     public function deleteAllImgProduct($arrid)
     {
     	$where = new In('ID_PR',$arrid);
     	$this->tableGateway->delete($where);
     }

     public function selectImgProduct($arrid)
     {
     	$where = new In('ID_PR',$arrid);
     	$resultSet = $this->tableGateway->select($where);
     	$resultSet = $resultSet->toArray();
     	$arr = array();
     	foreach ($resultSet as $val)
     		$arr[] = $val['LINKIMG'];
     	return $arr;
     }
     
     public function deleteImgProductInType($arrid)
     {
     	$where = new In('ID_PR',$arrid);
     	$this->tableGateway->delete($where);
     }
 }